/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */
export default function access(initialState: { currentUser?: API.CurrentUser | undefined }) {
  const { currentUser } = initialState || {};

  const hasPermission = (permissionCode: string) => {
    return (
      (currentUser &&
        currentUser.permissions?.map((permission) => permission.code).indexOf(permissionCode) >
          -1) ||
      false
    );
  };

  return {
    canAdmin: currentUser && hasPermission('admin'),
    hasPermission,
    accesses: (currentUser && currentUser.permissions?.map((permission) => permission.code)) || [],
  };
}
